package edu.umn.actvs;


import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

public class ShowEventActivity extends Activity{

	private Intent intent;
	private Button b1, b2;
	private String[] tEvents;
	private List <CheckBox> chks = new ArrayList<CheckBox>();
	private EventDBCreator creator;


	private LinearLayout.LayoutParams LP_FF = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);  
	private LinearLayout.LayoutParams LP_FW = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);  
	private LinearLayout.LayoutParams LP_WW = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		creator = new EventDBCreator(this);

		final LinearLayout layout2 = new LinearLayout(this);
		layout2.setOrientation(LinearLayout.VERTICAL);

		intent = this.getIntent();
		String events = intent.getStringExtra("event");	

		if(events!=null)
		{
			StringTokenizer st = new StringTokenizer(events,":");			

			TextView tt = new TextView(this);
			tt.setText("Please select the event");
			layout2.addView(tt);
			int i=0;
			while(st.hasMoreTokens())
			{
				String currentEvent = st.nextToken();
				StringTokenizer st1 = new StringTokenizer(currentEvent,"|");
				String eventName = st1.nextToken();

				CheckBox chk=new CheckBox(this);
				chk.setText(eventName);
				chk.setLayoutParams(LP_FW);
				chk.setId(i);
				chks.add(chk);
				i++;
				layout2.addView(chk, LP_FW);
			}	

			b1 = new Button(this);
			b2 = new Button(this);
			b1.setText("Join Event");
			b2.setText("Create");

			b1.setLayoutParams(LP_WW);
			b2.setLayoutParams(LP_WW);

			LinearLayout myLayout = new LinearLayout(this);
			myLayout.addView(b1);
			myLayout.addView(b2);
			layout2.addView(myLayout);


			b1.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View arg0) {
					for(Iterator<CheckBox> it = chks.iterator();it.hasNext();){
						CheckBox currentChk = it.next();

						String checkedEvents = null;
						if(currentChk.isChecked()){
							//TODO  add event handler
							checkedEvents+=currentChk.getId()+":";
							Log.w("check",currentChk.getText().toString());
							Log.w("check",Integer.toString(currentChk.getId()));
							
							//TODO send "join" Command to the master, get the port, username, and password
							//Insert the event into the local datebase
							
							
			//				String ev = currentChk.getText().toString()+",5001,test,test";
			//				creator.insertEvent(ev);
							String dirName = "/sdcard/"+currentChk.getText().toString()+"/";
							File newDir = new File(dirName);
							if(!newDir.mkdir())
								Log.w("error","mkdir fails");
						}
						
						
						/*
						Intent in= new Intent();
						in.putExtra("selectedEvents", checkedEvents);
						
						setResult(RESULT_OK,in);		//return results  to MainPanel
						ShowEventActivity.this.finish();
						*/

					}
					String ev = "event03"+",5001,testuser3,testpass3";
					creator.insertEvent(ev);
				} 
			});


		}else
		{
			TextView tt = new TextView(this);
			tt.setText("No events available");
			layout2.addView(tt);


			b2 = new Button(this);
			b2.setText("Create");
			b2.setLayoutParams(LP_WW);

			layout2.addView(b2);
		}

		setContentView(layout2); 
		b2.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				// create new event
				Intent intent = new Intent(ShowEventActivity.this, CreateEventActivity.class);
				startActivity(intent);

			} 
		});


	}
}
